package com.hl.card.credit.pub.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * @author 905546
 * 
 */
public class PropertiesUtil {
	private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class.getName());
	private static Properties properties;
	static {
		synchronized (PropertiesUtil.class) {
			if(properties==null){
				try {
					properties = loadConfig();
					logger.info("加载配置文件成功，路径：application.properties");
				} catch (IOException e) {
					logger.error("获取 application.properties"+ "配置参数出错：", e);
				}
			}
		}
		
	}

	private static Properties loadConfig() throws IOException {
		Properties properties = new Properties();
		properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream("application.yml"),"utf-8"))
		;
		return properties;
	}

	/**
	 * 根据key获取value
	 * 
	 * @param key
	 * @return
	 */
	public static String getStringValue(String key) {
		return properties.getProperty(key);
	}
	/**
	 * 根据key获取value
	 * 
	 * @param key
	 * @return
	 */
	public static Integer getIntegerValue(String key) {
		 String value=properties.getProperty(key);
		 return NumberUtils.toInt(value);
	}
	
	/**
	 * 根据key获取value
	 * @param key
	 * @return
	 */
	public static Boolean getBooleanValue(String key) {
		 String value=properties.getProperty(key);
		 if(value==null){
			 return null;
		 }
		 return Boolean.parseBoolean(value);
	}
	/**
	 * 根据key获取value
	 * 
	 * @param key
	 * @return
	 */
	public static Long getLongValue(String key) {
		 String value=properties.getProperty(key);
		 if(value==null){
			 return null;
		 }
		 return Long.parseLong(value);
	}
	
	
	public static Long getLongValueNumber(String key) {
		 String value=properties.getProperty(key);
		 return NumberUtils.toLong(value);
	}
	
	public static String[] getStrArr(String key,String regex){
		 String value=properties.getProperty(key);
		 if(value==null){
			 return null;
		 }
		 return value.split(regex);
	}
	public static int[] getIntegrArr(String key,String regex){
		 String[] values=getStrArr(key,regex);
		 if(values==null){
			 return null;
		 }
		 int length=values.length;
		 int[] arr=new int[length];
		 for(int i=0;i<length;i++){
			 arr[i]=Integer.parseInt(values[i]);
		 }
		 return arr;
	}

	public static double getDoubleValue(String key) {
		// TODO Auto-generated method stub
		 String value=properties.getProperty(key);
		 return NumberUtils.toDouble(value);
	}
	
}
